package com.micro.service.impl;

import com.micro.entity.HotelOrder;
import com.micro.repository.HotelOrderRepository;
import com.micro.service.HotelOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class HotelOrderServiceImpl implements HotelOrderService {

    @Autowired
    private HotelOrderRepository hotelOrderRepository;

    @Override
    @Transactional
    public HotelOrder createOrder(HotelOrder order) {
        // 设置创建时间和过期时间
        order.setUpdateTime(LocalDateTime.now());
        order.setExpireTime(LocalDateTime.now().plusDays(1)); // 默认1天后过期
        hotelOrderRepository.insert(order);
        return order;
    }

    @Override
    public List<HotelOrder> getOrdersByUserId(Long userId) {
        return hotelOrderRepository.findByUserId(userId);
    }

    @Override
    public List<HotelOrder> getOrdersByHotelId(Long hotelId) {
        return hotelOrderRepository.findByHotelId(hotelId);
    }

    @Override
    public List<HotelOrder> getOrdersByOrderStatus(HotelOrder.OrderStatus orderStatus) {
        return hotelOrderRepository.findByOrderStatus(orderStatus.name());
    }

    @Override
    public List<HotelOrder> getOrdersByUserIdAndOrderStatus(Long userId, HotelOrder.OrderStatus orderStatus) {
        return hotelOrderRepository.findByUserIdAndOrderStatus(userId, orderStatus.name());
    }

    @Override
    public List<HotelOrder> getOrdersByCheckInDate(LocalDate checkInDate) {
        return hotelOrderRepository.findByCheckInDate(checkInDate);
    }

    @Override
    public List<HotelOrder> getOrdersByCheckOutDate(LocalDate checkOutDate) {
        return hotelOrderRepository.findByCheckOutDate(checkOutDate);
    }

    @Override
    public List<HotelOrder> getOrdersByHotelIdAndCheckInDateBetween(Long hotelId, LocalDate startDate, LocalDate endDate) {
        return hotelOrderRepository.findByHotelIdAndCheckInDateBetween(hotelId, startDate, endDate);
    }

    @Override
    public HotelOrder getOrderById(Long orderId) {
        return hotelOrderRepository.selectById(orderId);
    }

    @Override
    @Transactional
    public HotelOrder updateOrderStatus(Long orderId, HotelOrder.OrderStatus orderStatus) {
        HotelOrder order = getOrderById(orderId);
        if (order != null) {
            order.setOrderStatus(orderStatus);
            order.setUpdateTime(LocalDateTime.now());
            hotelOrderRepository.updateById(order);
            return order;
        }
        return null;
    }

    @Override
    @Transactional
    public HotelOrder refundOrder(Long orderId, java.math.BigDecimal refundAmount) {
        HotelOrder order = getOrderById(orderId);
        if (order != null) {
            order.setOrderStatus(HotelOrder.OrderStatus.已退款);
            order.setRefundAmount(refundAmount);
            order.setRefundTime(LocalDateTime.now());
            order.setUpdateTime(LocalDateTime.now());
            hotelOrderRepository.updateById(order);
            return order;
        }
        return null;
    }
}
